#!/usr/bin/perl
#conf_bond.cgi

require './network-lib.pl';
&ReadParse();

&header($text{'bonding_config'}, "", "bondingcfg", 0, 1);
print "<form action=save_bonding.cgi method=post>\n";
#print "<input type=hidden name=bonding_status value='$in{'bonding_status'}'>";
printf "&nbsp<input type=submit class=button1 value=%s onclick=change_bond_stat()>",(&check_bond_stat())?$text{'stop_bonding'}:$text{'start_bonding'};

print "<hr\n>";
if(!&check_bond_stat())
{
	printf "No Bonding Card\n";
}
else{

my @c_usertype = ($text{'local_user'}, $text{'local_group'}, $text{'pdc_user'}, $text{'pdc_group'});
print "<input type=hidden name=trustusercache value=\"$in{'trustusercache'}\">\n";
print "<table class=mode1 cellpadding=5 cellspacing=0 width=100%>\n";
print "<tr> <td valign=top><div class=small><table class=mode1 border=0 cellspacing=0 cellpadding=1 width=100%>\n";
print "<tr> <td colspan=1><b>$text{'bond0'}</b></td>\n";
$bond_ip=&get_bond_ip();
$bond_mask=&get_bond_mask();
print "<td colspan=1><input type=text name=bond_ip value=$bond_ip></td>";
print "<td colspan=1><input type=text name=bond_mask value=$bond_mask></td>";
print "<td colspan=1><input type=submit value=$text{'modify'} onclick=change_action_modify_ip()></td></tr>";

if(&check_bond0_ip()){
print "<tr class=title1> <th align=left>$text{'adapter'}</th> ",
	"<th align=left></th> ",
	"<th align=left></th> </tr>\n";
#@adapters = split(":", $in{'trustusercache'});
&read_file_byline("/etc/bond.conf",\@adapters);
$n = @adapters;
for ($i = 0; $i < @adapters; $i ++)
{
	if ($i % 2 eq 1)
	{
		$dismode = "class=mode2";
	}
	else
	{
		$dismode = "class=mode1";
	}
	my @thlist = split(',', $adapters[$i]);
	print "<tr $dismode> <td><input type=radio name=del_slave_card value='$adapters[$i]'>$adapters[$i]</td>\n";

#	print "<td><select name=cifsaccessitem class=drop onchange='cachecifslist($n)'>\n";
#	printf "<option %s value='0'>$text{'readonly'}\n", ($thlist[2] eq "0")?"selected":"";
#	printf "<option %s value='1'>$text{'readwrite'}\n", ($thlist[2] eq "1")?"selected":"";
#	printf "<option %s value='2'>$text{'denyaccess'}\n", ($thlist[2] eq "2")?"selected":"";
#	print "</select></td>\n";
#	print "<td>$c_usertype[$thlist[0]]</td> </tr>\n";
}

#		if ($n % 2 eq 1)
#			{
#				$dismode = "class=mode2";
#			}
#			else
#			{
#				$dismode = "class=mode1";
#			}
#		print "<tr $dismode><td><b>$text{'other_users'}</b></td>\n";
#		print "<td colspan=2><select name=othersel class=drop>\n";
#		printf "<option %s value='0'>$text{'readonly'}\n", ($in{'othersel'} eq "0")?"selected":"";
#		printf "<option %s value='1'>$text{'readwrite'}\n", ($in{'othersel'} eq "1")?"selected":"";
#		printf "<option %s value='2'>$text{'denyaccess'}\n", ($in{'othersel'} eq "2")?"selected":"";
#		print "</select></td> </tr>\n";

		print "</table></div></td>\n";
		print "<td><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
#		print "<tr> <td align=center><b>$text{'bonding_config'}</b>\n";
#		print "<select name=cifsaccesssel>\n";
#		print "<option value='0'>$text{'readonly'}\n";
#		print "<option value='1'>$text{'readwrite'}\n";
#		print "<option value='2'>$text{'denyaccess'}\n";
#		print "</select></td> </tr>\n";
		print "<tr><td align=center><input type=submit name=addcifsuserbtn value=\"$text{'add'}\" onclick='addbondslave();' class=button1></td> </tr>\n";
		print "<tr> <td align=center><input type=submit name=removecifsuserbtn value=\"$text{'remove'}\" onclick='removebondslave();' class=button1></td></tr>\n";
		print "</table></td>\n";

		print "<td valign=top width=200><table class=mode1 border=0 cellspacing=0 cellpadding=1 width=100%>\n";
		print "<tr> <td><b>$text{'available_card'}</b></td> </tr>\n";
#		print "<tr> <td><select name=usertyperdn onchange='change_user_list();'>\n";
#		printf "<option %s value='0'>$text{'show_localuser'}\n", ($in{'usertyperdn'} eq "0")?"selected":"";
#		printf "<option %s value='1'>$text{'show_localgroup'}\n", ($in{'usertyperdn'} eq "1")?"selected":"";
#		printf "<option %s value='2'>$text{'show_pdcuser'}\n", ($in{'usertyperdn'} eq "2")?"selected":"";
#		printf "<option %s value='3'>$text{'show_pdcgroup'}\n", ($in{'usertyperdn'} eq "3")?"selected":"";
#		printf "</select></td></tr>\n";
		&read_file_byline("/etc/modules.conf",\@lref);
		foreach $line (@lref)
	 	{
	 		if(!($line=~/^#/))
	 		{
		 		if($line=~/(eth\d+)/)
		 		{
		 			if(!&check_slave($1))
		 			{
		 				push(@slave,$1);
		 			}
		 		}
	 		}
	 	}
		print "<tr> <td><select name=add_slave_card size=10 class=list>\n";
	 	for($i=0;$i<@slave;$i++)
	 	{
	 		print "<option value='$slave[$i]'>$slave[$i]\n";
	 	}
		print "</select></td> </tr>\n";

		print "</table></td> </tr>\n";

#		print "<tr> <td colspan=3><b>$text{'max_connections'}</b>\n";
#		printf "<input type=radio name=maxconnect %s value='0' onclick='changeconnection();'>$text{'unlimit'}\n", ($in{'maxconnect'} =~ /0/)?"checked":"";
#		printf "<input type=radio name=maxconnect %s value='1' onclick='changeconnection();'><input name=maxconnectionin %s class=%s size=5 value='$in{'maxconnectionin'}'></td> </tr>\n", ($in{'maxconnect'} =~ /1/)?"checked":"", ($in{'maxconnect'} =~ /1/)?"":"disabled=true", ($in{'maxconnect'} =~ /1/)?"text":"disabletext";

		print "</table><p>\n";
}#if(&check_bond0_ip())
}#if(!&check_bond_stat())
&footer("index.cgi", $text{'interface_return'});

sub read_file_byline()
{
        my ($ptf, $filename);

        $filename = @_[0];
        $ptf = @_[1];

        open(RFILE, $filename);
        @$ptf = <RFILE>;
        close(RFILE);
}

sub check_bond_stat()
{
	$bonding = "off";
	`/sbin/lsmod >/tmp/bond`;

 	local @lref;
 	&read_file_byline("/tmp/bond",\@lref);
 	foreach $line (@lref)
 	{
 		if($line=~/bonding/)
 		{
 			$bonding = "on";
 			last;
 		}
 	}
 	`rm -f /tmp/bond`;
 	if($bonding eq "on")
 	{
		return 1;
	}
	else
	{
		return 0;
	}
}
sub check_bond0_ip()
{
	$bonding = "off";
	`/sbin/ifconfig >/tmp/bond0`;

 	local @lref;
 	&read_file_byline("/tmp/bond0",\@lref);
 	foreach $line (@lref)
 	{
 		if($line=~/bond0/)
 		{
 			$bonding = "on";
 			last;
 		}
 	}
 	`rm -f /tmp/bond0`;
 	if($bonding eq "on")
 	{
		return 1;
	}
	else
	{
		return 0;
	}
}
sub get_bond_ip()
{
	$bonding = "off";
	$bond_ip = "";
	`/sbin/ifconfig >/tmp/bond0`;

 	local @lref;
 	&read_file_byline("/tmp/bond0",\@lref);
 	for($i=0;$i<@lref;$i++)
 	{
 		if($lref[i]=~/bond0/)
 		{
 			$bonding = "on";
 			last;
 		}
 	}
 	if($bonding eq "on")
 	{
 		if($lref[i+1]=~/addr:(\d+\.\d+\.\d+\.\d+)/)
 		{
 			$bond_ip=$1;
 		}
 	}
 	`rm -f /tmp/bond0`;
 	if($bonding eq "on")
 	{
# 		print $bond_ip;
		return $bond_ip;
	}
	else
	{
		return;
	}
}
sub get_bond_mask()
{
	$bonding = "off";
	$bond_mask = "";
	`/sbin/ifconfig >/tmp/bond0`;

 	local @lref;
 	&read_file_byline("/tmp/bond0",\@lref);
 	for($i=0;$i<@lref;$i++)
 	{
 		if($lref[i]=~/bond0/)
 		{
 			$bonding = "on";
 			last;
 		}
 	}
 	if($bonding eq "on")
 	{
 		if($lref[i+1]=~/Mask:((\d+)\.(\d+)\.(\d+)\.(\d+))/)
 		{
 			$bond_mask=$1;
 		}
 	}
 	`rm -f /tmp/bond0`;
 	if($bonding eq "on")
 	{
		return $bond_mask;
	}
	else
	{
		return;
	}
}
sub check_slave()
{
	local @lref;
	&read_file_byline("/etc/bond.conf",\@lref);
	foreach $line (@lref)
 	{
 		if($line=~/$_[0]/)
 		{
 			return 1;
 		}
 	}
 	return 0;
}
print <<EOF;
<script>
function change_bond_stat()
{
	document.forms[0].action = 'change_bond_stat.cgi';	
}
function change_action_modify_ip()
{
	document.forms[0].action = 'modify_ip.cgi';	
}
function addbondslave()
{
	document.forms[0].action = 'add_slave.cgi';
}
function removebondslave()
{
	document.forms[0].action = 'remove_slave.cgi';
}
</script>
EOF
